package com.example.mymvvmdemo.models.main.fragment.home.adapter;

import android.content.Context;
import android.view.View;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseVBAdapter;
import com.example.mymvvmdemo.databinding.ItemHomeBinding;
import com.example.mymvvmdemo.entity.CommonEntity;
import com.example.mymvvmdemo.models.main.fragment.home.entity.BlogArticlesEntity;
import com.example.mymvvmdemo.utils.GlideUtils;
import com.example.mymvvmdemo.utils.StrUtils;

import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends BaseVBAdapter<BlogArticlesEntity.DataDTO, ItemHomeBinding> {

    private HomeListener listener;

    public void setListener(HomeListener listener) {
        this.listener = listener;
    }

    /**
     * @param context
     */
    public HomeAdapter(Context context, ArrayList<BlogArticlesEntity.DataDTO> list) {
        super(context);
        setDataList(list);
    }

    @Override
    protected void updateData(ItemHomeBinding bind, BlogArticlesEntity.DataDTO item, int position) {

        if (item == null) {
            return;
        }

        String title = item.getTitle();
        if (!StrUtils.isEmpty(title)) {
            bind.tvTitle.setText(title);
        }

        String desc = item.getContent();
        if (!StrUtils.isEmpty(desc)) {
            bind.tvDesc.setText(desc);
        }

        String time = item.getCreated_at();
        if (!StrUtils.isEmpty(time)) {
            bind.tvTime.setText(time);
        }

        String imageUrl = item.getImage_url();
        GlideUtils.loadImage(context, imageUrl, R.mipmap.app_icon, bind.ivHome);

        bind.clAll.setOnClickListener(view -> {
            if (listener != null) {
                listener.onHomeClick(position);
            }
        });

    }


    public interface HomeListener {
        void onHomeClick(int position);
    }

}
